//-sdcard /home/max/android/android-sdk-linux_x86-1.0_r2/tools/sdimg

package android.com.app;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.google.android.maps.GeoPoint;

public class thumbviewer extends Activity {
    /** Called when the activity is first created. */
	
	private Context mContext;
	
	@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.apicthumbsview);

	        GridView g = (GridView) findViewById(R.id.grid1);
	        final ImageAdapter ia = new ImageAdapter(this);
	        g.setAdapter(ia);
	        g.setOnItemClickListener(new OnItemClickListener() {
	        
	        public void onItemClick(AdapterView parent, View v, int position, long id) {
	            		ia.callImageViewer(position);
	            }
	        });
	    }
	
	protected void onPause() {
    	super.onPause();
    	//finish();
    }
	private static final int CAM_ID = Menu.FIRST;
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        
        menu.add(0, CAM_ID, 0,  "Camera");
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case CAM_ID:
        	startActivity(new Intent( this, aPics.class));
        	return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
	
    public class ImageAdapter extends BaseAdapter {
    	
    	private String imgData;
	    private String geoData;
	    private ArrayList<String> thumbsDataList;
	    private ArrayList<String> thumbsIDList;
	    
	    //private Bitmap bmp;
    	ImageAdapter(Context c) {
            mContext = c;
           
            thumbsDataList=new ArrayList<String>();
            thumbsIDList=new ArrayList<String>();
            getThumbInfo(thumbsIDList,thumbsDataList);
        }
        
    	public boolean deleteSelected(int sIndex){
    		
    	//	getContentResolver().delete(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+imgList.get(sIndex)), null, null);
    	//	getContentResolver().delete(Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""+imgList.get(sIndex)), null, null);
    
    		
    		return true;
    	}
    	 
    	//This loads the image viewer activity
        
        public final void callImageViewer(int selectedIndex){
       
	        TextView t = (TextView) findViewById(R.id.txt1);
	       
        	Intent i = new Intent();
        	i.setClass(mContext, imageviewer.class);
        	getImageInfo(imgData,geoData,thumbsIDList.get(selectedIndex));
        	i.putExtra("GeoPoint",geoData);
        	i.putExtra("Title", imgData);
        	startActivityForResult(i,1);  
        	
        }
        
        public int getCount() {
            return thumbsIDList.size();
        	       }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        //Load images in gridview
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85,85));
                imageView.setAdjustViewBounds(false);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            
            TextView t = (TextView) findViewById(R.id.txt1);
            System.gc();
            try{

            	BitmapFactory.Options bo = new BitmapFactory.Options();
    			bo.inSampleSize=8;   
            	Bitmap bmp = BitmapFactory.decodeFile(thumbsDataList.get(position),bo);
            	//imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+thumbsIDList.get(position))); 
            	//imageView.setImageURI(Uri.parse(thumbsIDList.get(position)));
            	//Log.i("THUMBNAILS DATA" ,Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, titleList.get(position)).toString());
            	imageView.setImageBitmap(bmp);
            	//bmp.recycle();
            	System.gc();
            }
            catch(Exception e){
            	t.setText(e.getMessage());
            	
            }
           
            return imageView;
        }

        /* Get all thumbnails from content provider in an array list for display in grid*/
        public void getThumbInfo(ArrayList<String> ThumbsIDList, ArrayList<String> ThumbsDataList){
                     	
	        TextView t = (TextView) findViewById(R.id.txt1);
        	String[] imageAttribs = new String[] {
     				Images.Thumbnails._ID,
      				Images.Thumbnails.DATA,
      				Images.Thumbnails.IMAGE_ID,
      				Images.Thumbnails.WIDTH,
        	};
  
        	Cursor imageCursor= managedQuery(
        			MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        			imageAttribs, 
        			null,
                    null,          // WHERE clause.
                    null);
        	
        	if (imageCursor !=null && imageCursor.moveToFirst()) {
        		
                String title; 
                String thumbsID;
                String thumbsImageID;
                String thumbsData;
                String data;
                String thumbsWidth;
                
                int thumbsIDCol = imageCursor.getColumnIndex(Images.Thumbnails._ID);
                int thumbsDataCol = imageCursor.getColumnIndex(Images.Thumbnails.DATA);
                int thumbsImageIDCol = imageCursor.getColumnIndex(Images.Thumbnails.IMAGE_ID);
                int thumbsWidthCol = imageCursor.getColumnIndex(Images.Thumbnails.WIDTH);
                int num=0;
                do {
                    // Get the field values
                   		thumbsID = imageCursor.getString(thumbsIDCol);
                		thumbsData = imageCursor.getString(thumbsDataCol);
                		thumbsImageID = imageCursor.getString(thumbsImageIDCol);
                		thumbsWidth = imageCursor.getString(thumbsWidthCol);

                	   // Log.i("BMP","size "+ thumbsWidth+" "+thumbsID+" "+thumbsData+" "+thumbsImageID);  
                		num++;
                		if(thumbsImageID!= null) {
                			ThumbsIDList.add(imageCursor.getString(thumbsImageIDCol));
                			ThumbsDataList.add(imageCursor.getString(thumbsDataCol));
                			//ThumbsDataList.add(imageCursor.getString(thumbsDataCol));
                		}
                } while (imageCursor.moveToNext());            
        	}
        	imageCursor.close();
    	 return;
    	 
        }
        
        //Return the image info corresponding to selected thumbnail. need this to pass thru intents
        
        public void getImageInfo(String ImageData,String Location, String thumbID){
         	
        	String[] imageAttribs = new String[] {
      				Images.ImageColumns.DATA,
      				Images.ImageColumns.LATITUDE,
      				Images.ImageColumns.LONGITUDE,
      				
        	};
  
        	Cursor imageCursor= managedQuery(
        			MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        			imageAttribs, 
        			"_ID='"+thumbID+"'",
                    null,          // WHERE clause.
                    null);
        	
        	
           	if (imageCursor !=null && imageCursor.moveToFirst()) {
        		
                          
                int imgDataCol = imageCursor.getColumnIndex(Images.ImageColumns.DATA); 
                int LatCol = imageCursor.getColumnIndex(Images.ImageColumns.LATITUDE);
                int LongCol =  imageCursor.getColumnIndex(Images.ImageColumns.LONGITUDE);
              
                int num=0;
                do {
                    // Get the field values
                		if(num>0){
                			break;
                		}
                		
                		imgData = imageCursor.getString(imgDataCol);
                		//Log.i("imgda",ImageData);
                		
                		//Comment this for emu
                		//LocationList.add(new GeoPoint((int)34.027412*1000000,(int)-118.289738*1000000));
                		//Comment this for device
                		geoData = (imageCursor.getDouble(LatCol))+","+(imageCursor.getDouble(LongCol));
                		Log.i("gallery",geoData);
                			num++;	
                		
                } while (imageCursor.moveToNext());   
        	}
           	imageCursor.close();
    	 return;
        }
        
   
    }//class imageadapter

    
    protected void onDestroy() {
    	super.onDestroy();
    	//finish();
    }


}//class apicsthumbview>>>>>>> .r67
